package com.apimarket.controller;

import com.apimarket.entity.DataPacket;
import com.apimarket.entity.Dataset;
import com.apimarket.entity.Result;
import com.apimarket.entity.vo.DataSetAndPacketVo;
import com.apimarket.service.DataSetService;
import com.apimarket.util.RedisUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author ZXR
 * @Date 2021/7/6 15:53
 * @Version 1.0
 */

@RequestMapping("/dataset")
@RestController
public class DataSetController {

    @Autowired
    private DataSetService dataSetService;
    @Value("${redis.dataset.type}")
    private String redisDataSetType;
    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("/test/{url}")
    public void testUrl(@PathVariable String url){
        System.out.println(url);
    }

    /*@RequestMapping(value = "/download/ceshi2")
    public void downloadCeShi(@RequestBody Map<String,String> temp){
        System.out.println(temp);
    }*/

    /**
     * 下载指定URL中的数据包资源
     * @param datapacketId
     * @param url 这里的url是服务器的本地资源路径
     * @return
     */
    @RequestMapping(value = "/download/{datapacketId}",method = {RequestMethod.GET})
    public String downloadPacket(@PathVariable String datapacketId, String url, HttpServletRequest request , HttpServletResponse response){
        try {
            dataSetService.downloadPacket(datapacketId,url,request,response);
            dataSetService.addDownloadNum(datapacketId);
            return "下载成功";
            //return new Result<>("下载成功",200,"success");
        } catch (IOException e) {
            e.printStackTrace();
            return "下载失败";
            //return new Result<>("下载失败",200,"fail");
        }

    }


    /**
     * 判断当前用户对这个数据块的申请是否通过
     * @param datasetId
     * @return
     */
    @RequestMapping("judegeHaveRightToDownload")
    public Result<Boolean> judegeHaveRightToDownload(String datasetId,String userId){
        Boolean hasRight=dataSetService.judegeHaveRightToDownload(datasetId,userId);
        if(hasRight){
            return new Result<>(hasRight,200,"您对该数据块的使用申请已通过，请直接下载");
        }else{
            return new Result<>(hasRight,400,"请先申请数据块");
        }
    }

    /**
     * 返回数据块和属于他的数据包的信息
     * @param id
     * @return
     */
    @RequestMapping("detailAndChildPacket")
    public Result<DataSetAndPacketVo> getDataSetDetailAndChildPackets(String id){
        DataSetAndPacketVo result=dataSetService.getDataSetDetailAndChildPackets(id);
        return new Result<>(result,200,"success");
    }

    @RequestMapping("/packet/delete")
    public Result<String> deletPacket(String id){
        dataSetService.deletPacket(id);
        return new Result<>("删除成功",200,"success");
    }

    @RequestMapping(value = "/packet/update",method = {RequestMethod.POST})
    public Result<String> updateDataPacket(@RequestBody DataPacket packet){
        dataSetService.updateDataPacket(packet);
        return new Result<>("更新数据包成功",200,"success");
    }

    @RequestMapping("/packet/getPageObject")
    public Result<PageInfo<DataPacket>> getPacketPageObjct(Integer pageSize,Integer page,String name,String datasetId){
        PageInfo<DataPacket> result=dataSetService.getPacketPageObject(pageSize,page,name,datasetId);
        return new Result<>(result,200,"success");
    }

    @RequestMapping(value = "/packet/add",method = {RequestMethod.POST})
    public Result<String> packetAdd(@RequestBody @Validated DataPacket packet){
        dataSetService.packetAdd(packet);
        return new Result<String>("新增数据包成功",200,"success");
    }

    /**
     * 删除dataset
     * @param id
     * @return
     */
    @RequestMapping("deleteDataSet")
    public Result<String> deleteDateSet(String id){
        dataSetService.deleteDataSet(id);
        return new Result<>("删除数据块成功",200,"success");
    }

    @RequestMapping("/getTypeList")
    public Result<List<Map<Integer,String>>> getTypeList(){
        List<Map<Integer,String>> map=null;
        map= (List<Map<Integer, String>>) redisUtils.judgeAndGet(redisDataSetType);
        if(map==null) {
            map = dataSetService.getTypeList();
            redisUtils.set(redisDataSetType,map);
        }
        return new Result<>(map,200,"success");
    }

    /**
     * 编辑数据块
     * @param dataset
     * @param picFile
     * @param exampleFile
     * @return
     */
    @RequestMapping(value = "updateDataset",method = {RequestMethod.POST})
    public Result<String> updateDataset(Dataset dataset, MultipartFile picFile, MultipartFile exampleFile){
        dataSetService.updateDataset(dataset,picFile,exampleFile);
        return new Result<>("编辑数据块成功",200,"success");
    }

    @RequestMapping(value = "addDataset",method = {RequestMethod.POST})
    public Result<String> addDataset(@Validated Dataset dataset, MultipartFile picFile, MultipartFile exampleFile) throws URISyntaxException {
        try {
            dataSetService.addDataset(dataset,picFile,exampleFile);
            return new Result<>("添加数据块成功",200,"success");
        } catch (IOException e) {
            return new Result<>("添加数据块失败",400,"fail");
        }

    }

    /**
     * 分页返回数据块的数据
     * @param type
     * @param name
     * @param pageSize
     * @param page
     * @return
     */
    @RequestMapping("getPageObject")
    public Result<PageInfo<Dataset>> getPageObject(Integer type, String name,Integer pageSize,Integer page){
        PageInfo<Dataset> result=dataSetService.getPageObject(type,name,pageSize,page);
        return new Result<>(result,200,"success");
    }
}
